﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Management;
using ListNetworkComputers;
using System.IO;
using System.Collections;
using System.Threading;

namespace ManagerHmsshield
{
    public partial class Principal : Form
    {
        public Principal()
        {
            InitializeComponent();
            Tabela tb = new Tabela("his_host");
            tb.refreshTable();
            foreach (DataRow r in tb.dt.Rows)
            {
                ListViewItem lvi = new ListViewItem(r[0].ToString());
                his_pc.Items.Add(lvi);
            }
        }

        private void Principal_Shown(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            listPcs();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ckAutomatico.Checked)
            {
                listPcs();
            }
        }

        private void listPcs()
        {
            pcs.Items.Clear();
            NetworkBrowser list = new NetworkBrowser();
            ArrayList networkComputers = list.getNetworkComputers();
            foreach (String str in networkComputers)
            {
                ListViewItem lvi = new ListViewItem(str);
                pcs.Items.Add(lvi);
            }

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            arq.Items.Clear();
            GetNetShares get = new GetNetShares();
            foreach (GetNetShares.SHARE_INFO_1 info in get.EnumNetShares(nameServ.Text))
            {
                ListViewItem lvi = new ListViewItem(info.ToString());
                arq.Items.Add(lvi);
            }
        }

        private void pcs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pcs.SelectedItems.Count != 0)
            {
                nameServ.Text = pcs.SelectedItems[0].Text;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            atualizarLista();
        }

        public void atualizarLista()
        {
            NetworkBrowser list = new NetworkBrowser();
            ArrayList networkComputers = list.getNetworkComputers();
            Boolean atualizar = false;

            Tabela tb = new Tabela("his_host");
            foreach (String str in networkComputers)
            {
                if (tb.SelectTable(string.Format("name_host = '{0}'", str)).Rows.Count == 0)
                {
                    tb.insert(str);
                    atualizar = true;
                }
            }

            if (atualizar)
            {

                tb.refreshTable();
                foreach (DataRow r in tb.dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem(r[0].ToString());
                    his_pc.Items.Add(lvi);
                }
            }
        }

    }
}
